<?php

    #-------------------------------------------------------------#
	# This file is part of the Tekuna MVC Microframework for PHP. #
	# (c) 2009 Niels Jäckel <niels.jaeckel@tekuna.org>            #
	# Please visit http://tekuna.org/ for more information.       #
	#                                                             #
	# This software is licensed under the MIT License. Please     #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#


	Tekuna :: load('org.tekuna.core.config.TekunaConfigurationElement');
	Tekuna :: load('org.tekuna.core.config.ApplicationModule');
	Tekuna :: load('org.tekuna.core.config.NoModuleRoutableException');
	Tekuna :: load('org.tekuna.core.config.InvalidConfigurationException');


	/**
	 * Representation class that holds the a complete configured Tekuna configuration.
	 * Each configuration consists of one or more supported languages and some Modules.
	 */

	class ApplicationConfiguration implements TekunaConfigurationElement {

		protected
			$arrLanguages = array(),
			$arrModules = array();


		/**
		 * Checks if this instance of a component is validly configured.
		 *
		 * @return boolean TRUE if valid
		 */

		public function isValid() {

			return (count($this -> arrLanguages) > 0) && (count($this -> arrModules) > 0);
		}


		/**
		 * @return array returns an array with the defined languages.
		 */

		public function setLanguages(array $arrLanguages) {

			$this -> arrLanguages = $arrLanguages;
		}


		/**
		 * @return array returns an array with the defined languages.
		 */

		public function getLanguages() {

			return $this -> arrLanguages;
		}


		/**
		 * Add a component to this module. If the component is not correct configured,
		 * a InvalidConfigurationException is thrown.
		 *
		 * @param ApplicationComponent $objComponent the new component object
		 */

		public function addModule(ApplicationModule $objModule) {

			if (! $objModule -> isValid()) {

				throw new InvalidConfigurationException("The given module is not correctly configured.");
			}

			$this -> arrModules[] = $objModule;
		}


		/**
		 * @return array returns an array of all modules as ApplicationModule objects.
		 */

		public function getAllModules() {

			return $this -> arrModules;
		}


		/**
		 * Try to find a module that matches the given action URL. If no module
		 * matches the given URL a NoModuleRoutableException is thrown.
		 *
		 * @param string $sActionURL
		 * @return ApplicationModule object
		 */

		public function getModuleForURL($sActionURL) {

			// iterate all modules
			foreach ($this -> arrModules as $objModule) {

				// extract the module's base URL
				$sModuleBaseURL = $objModule -> getBaseURL();

				// check if the base URL equals the
				// first part of the action URL
				if (strpos($sActionURL, $sModuleBaseURL) === 0) {

					return $objModule;
				}
			}

			throw new NoModuleRoutableException("There is no module defined that fits the request action '$sActionURL'.");
		}
	}

